const express = require('express');
const router = express.Router();
const reviewController = require('../controllers/reviewController');
const { authenticateToken } = require('../middlewares/authMiddleware');

/**
 * @swagger
 * tags:
 *   name: Reviews
 *   description: 影评相关的 API
 */

// 搜索评论
/**
 * @swagger
 * /reviews/search:
 *   post:
 *     summary: 搜索评论（带分页）
 *     tags: [Reviews]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               content:
 *                 type: string
 *                 example: "精彩"
 *               begin:
 *                 type: string
 *                 format: date
 *                 example: "2023-01-01"
 *               end:
 *                 type: string
 *                 format: date
 *                 example: "2023-12-31"
 *               page:
 *                 type: integer
 *                 default: 1
 *               limit:
 *                 type: integer
 *                 default: 15
 *     responses:
 *       200:
 *         description: 搜索成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 content: "非常精彩的电影"
 *                 likes: 50
 *       500:
 *         description: 服务器错误
 */
router.post('/search', reviewController.searchReviews);

// 根据ID获取评论
/**
 * @swagger
 * /reviews/{id}:
 *   get:
 *     summary: 根据ID获取评论详情
 *     tags: [Reviews]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取评论
 *         content:
 *           application/json:
 *             example:
 *               id: 1
 *               content: "经典必看电影"
 *               user_rating: 9.0
 *               likes: 100
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 服务器错误
 */
router.get('/:id', reviewController.getReviewsById);

//根据电影ID获取评论
/**
 * @swagger
 * /reviews/movies/{movieId}:
 *   get:
 *     summary: 根据电影ID获取评论
 *     tags: [Reviews]
 *     parameters:
 *       - in: path
 *         name: movieId
 *         required: true
 *         schema:
 *           type: integer
 *       - in: query
 *         name: sort
 *         schema:
 *           type: string
 *           enum: [created_at, hot]
 *           default: created_at
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 content: "特效震撼"
 *                 created_at: "2023-05-01T08:00:00Z"
 *       400:
 *         description: 无效的排序类型
 *       500:
 *         description: 服务器错误
 */
router.get('/movies/:movieId', reviewController.getReviewsByMovieId);

// 获取评论（带分页和排序）
/**
 * @swagger
 * /reviews:
 *   post:
 *     summary: 获取评论列表（分页排序）
 *     tags: [Reviews]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               page:
 *                 type: integer
 *                 default: 1
 *               pageSize:
 *                 type: integer
 *                 default: 10
 *               sort:
 *                 type: string
 *                 default: "likes"
 *               order:
 *                 type: string
 *                 enum: [ASC, DESC]
 *                 default: "DESC"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 content: "演员演技在线"
 *                 likes: 200
 *       500:
 *         description: 服务器错误
 */
router.post('/', reviewController.getReviewsWithPost);

//添加评论
/**
 * @swagger
 * /reviews/add:
 *   post:
 *     summary: 添加评论（需登录）
 *     tags: [Reviews]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - movie_id
 *               - content
 *             properties:
 *               movie_id:
 *                 type: integer
 *                 example: 1
 *               user_rating:
 *                 type: number
 *                 maximum: 10
 *                 minimum: 0
 *                 example: 8.5
 *               content:
 *                 type: string
 *                 example: "剧情非常吸引人"
 *     responses:
 *       201:
 *         description: 评论创建成功
 *         content:
 *           application/json:
 *             example:
 *               status: 201
 *               data: { reviewId: 5 }
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.post('/add', authenticateToken, reviewController.addReview);

//删除评论
/**
 * @swagger
 * /reviews/{id}:
 *   delete:
 *     summary: 删除评论（需登录）
 *     tags: [Reviews]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             example:
 *               message: "评论删除成功"
 *       403:
 *         description: 没有操作权限
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 服务器错误
 */
router.delete('/:id', authenticateToken, reviewController.deleteReview);

// 更新评论
/**
 * @swagger
 * /reviews/{id}:
 *   put:
 *     summary: 更新评论（需登录）
 *     tags: [Reviews]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               user_rating:
 *                 type: number
 *                 example: 9.0
 *               content:
 *                 type: string
 *                 example: "更新后的评论内容"
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             example:
 *               message: "评论更新成功"
 *       403:
 *         description: 没有操作权限
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/:id', authenticateToken, reviewController.updateReview);

// 点赞评论
/**
 * @swagger
 * /reviews/{id}/like:
 *   post:
 *     summary: 点赞评论（需登录）
 *     tags: [Reviews]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 点赞成功
 *         content:
 *           application/json:
 *             example:
 *               message: "点赞成功"
 *               likes: 150
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 服务器错误
 */
router.post('/:id/like', authenticateToken, reviewController.likeReview);

module.exports = router;